﻿using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization;
using BaseTypesLib.Model;
using System.Xml.Serialization;
using System.Xml.Linq;
using Microsoft.Practices.Unity;
using System.Linq;

namespace BBInterfaceNET.CommonServices
{
    public class FileDesignerPersistenceService:IDesignerPersistenceService
    {
        public Field LoadFile(string filePath)
        {
            MainScreen screen = null;
            try
            {
                XElement elem = XElement.Load(filePath);
                screen = (MainScreen)Field.FromXml(elem);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error loading file: " + ex.Message);
            }
            return screen;
        }


        public bool Save(string filePath, MainScreen data)
        {
            bool res = false;
            try
            {
                data.GetXml().Save(filePath, SaveOptions.None);
                res = true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error saving file: " + ex.Message);
            }
            return res;
        }


        public bool WriteInitialFileData(string filePath)
        {
            return Save(filePath, new MainScreen());
        }
    }
}
